package com.leemz.ch2.jdbc.secondedition;

import com.leemz.ch2.jdbc.entity.Student;
import com.leemz.ch2.jdbc.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用工具类简化业务类的代码
 */
public class StudentServiceSecond {
    public void save(Student student) {
        String sql = "insert into t_student(name, age) values(?,?)";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = JdbcUtil.getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, student.getName());
            pstmt.setObject(2, student.getAge());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, pstmt, connection);
        }
    }

    public void delete(int id) {
        String sql = "delete from t_student where id = ?";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = JdbcUtil.getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, id);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, pstmt, connection);
        }
    }

    public void update(Student student) {
        String sql = "update t_student set name=?, age=? where id=?";
        Connection connection = null;
        PreparedStatement pstmt = null;
        try {
            connection = JdbcUtil.getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, student.getName());
            pstmt.setObject(2, student.getAge());
            pstmt.setObject(3, student.getId());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, pstmt, connection);
        }
    }

    public Student get(int id) {
        String sql = "select * from t_student where id = ?";
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            connection = JdbcUtil.getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            if(rs.next()) {
                String name = rs.getString("name");
                int age = rs.getInt("age");
                Student stu = new Student(name, age);
                stu.setId(id);
                return stu;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt, connection);
        }
        return null;
    }

    public List<Student> list() {
        List<Student> list = new ArrayList<>();
        String sql = "select * from t_student";
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            connection = JdbcUtil.getConnection();
            pstmt = connection.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String name = rs.getString("name");
                int age = rs.getInt("age");
                Student student = new Student(name, age);
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt, connection);
        }
        return list;
    }
}
